/**
 * @Author: steinKuo
 * @Date:   2020-01-03 7:31 上午
 */
class Adjoin {
    constructor(vertex) {
        this.vertex = vertex;
        this.quantity = vertex.length;
        this.init();
    }

    init() {
        this.adjoinArray = Array.from({
            length: this.quantity * this.quantity
        });
    }

    container(vert, id) {
        let tem = -1
        vert.forEach((item, index) => {
            if (item.value_id === id.value_id) tem = index
        })

        return tem
    }

    getVertexRow(id) {
        const index = this.container(this.vertex, id)
        const col = []
        this.adjoinArray.forEach((item, pIndex) => {
            col.push(this.adjoinArray[index + this.quantity * pIndex]);
        });
        return col;
    }

    getAdjoinVertexs(id) {
        return this.getVertexRow(id).map((item, index) => (item ? this.vertex[index] : '')).filter(Boolean);
    }

    setAdjoinVertexs(id, sides) {
        const pIndex = this.container(this.vertex, id)
        sides.forEach((item) => {
            const index = this.container(this.vertex, item)
            this.adjoinArray[pIndex * this.quantity + index] = 1;
            // this.adjoinArray[pIndex][index] = 1
        });
    }

    getRowTotal(params) {
        params = params.map(id => this.getVertexRow(id));
        const adjoinNames = [];
        this.vertex.forEach((item, index) => {
            const rowtotal = params.map(value => value[index]).reduce((total, current) => {
                total += current || 0;
                return total;
            }, 0);
            adjoinNames.push(rowtotal);
        });
        return adjoinNames;
    }

    // 交集
    getUnions(params) {
        const row = this.getRowTotal(params);
        console.log(row, "jiaoji")
        let a = row.map((item, index) => item >= params.length && this.vertex[index]).filter(Boolean);
        console.log(a, "jiaoji")
        return a
    }

    // 并集
    getCollection(params) {
        params = this.getRowTotal(params);
        console.log(params, "params")
        let temp = params.map((item, index) => item && this.vertex[index]).filter(Boolean);
        return temp
    }
}

class ShopAdjoin extends Adjoin {
    constructor(commoditySpecs, data) {
        super(commoditySpecs.reduce((total, current) => [
            ...total,
            ...current.list,
        ], []));
        this.commoditySpecs = commoditySpecs;
        this.data = data;
        // 单规格矩阵创建
        this.initCommodity();
        // 同类顶点创建
        this.initSimilar();
        console.log(this.adjoinArray, "矩阵列表")
    }

    initCommodity() {
        this.data.forEach((item) => {
            this.applyCommodity(item.specs);
        });
    }

    initSimilar() {
        // 获得所有可选项
        const specsOption = this.getCollection(this.vertex);
        // console.log(specsOption,"specsOption")
        this.commoditySpecs.forEach((item) => {
            const params = [];
            item.list.forEach((value) => {
                if (this.container(specsOption, value) > -1) params.push(value);
            });
            // 同级点位创建

            this.applyCommodity(params);
        });
    }

    querySpecsOptions(params) {
        // 判断是否存在选项填一个
        if (params.some(Boolean)) {
            console.log("交集")
            params = this.getUnions(params.filter(Boolean));
        } else {
            console.log("并集")
            // params = this.getCollection(this.vertex);
        }
        console.log(params, "可选项")
        return params;
    }

    // 选出相应规格
    // querySpecs(params) {
    //     let pIndex = -1
    //     this.data.forEach((items, index) => {
    //         let tem = 0
    //         items.specs.forEach((item) => {
    //             tem = params.includes(item.value) ? tem + 1 : tem
    //         })
    //         if (tem === params.length) {
    //             pIndex = index
    //         }
    //     });
    //     console.log(pIndex, "pIndex")
    //     return pIndex
    // }

    applyCommodity(params) {
        params.forEach((param) => {
            this.setAdjoinVertexs(param, params);
        });
    }
}

export {
    ShopAdjoin
}